package com.example.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import com.example.utils.SearchResultManager.LogEntry;

import android.content.Context;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.text.format.Time;
import android.view.Gravity;
import android.widget.Toast;

public class Utils {

	public static String StringJoin(List<String> list, String delim) {
		StringBuilder buf = new StringBuilder();
		int num = list.size();
		for (int i = 0; i < num; i++) {
			if (i != 0)
				buf.append(delim);
			buf.append((String) list.get(i));
		}
		return buf.toString();
	}

	public static boolean isSameDay(Time t1, Time t2) {
		// Utils.Log(" T1 : " + Time.getJulianDay(t1.toMillis(true), 0) + " T2 "
		// + Time.getJulianDay(t2.toMillis(true), 0));
		return getDayFromTime(t1) == getDayFromTime(t2);
	}

	public static int getDayFromTime(Time t1) {
		return Time.getJulianDay(t1.toMillis(true), 0);
	}

	public static void ShowToast(Context context, int strSource,
			int sourceLength) {
		Toast toast = Toast.makeText(context, strSource, sourceLength);
		toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL,
				0, 0);
		toast.show();
	}

	static StackTraceElement[] stackTraceElements = null;

	public static void Log(String message) {
		stackTraceElements = Thread.currentThread().getStackTrace();
		String logStr = " # " + stackTraceElements[3].getMethodName() + " # ";
		android.util.Log.v(logStr, message);
	}

	public static void Log() {
		Log("");
	}

	public static Time getNow() {
		Time now = new Time();
		now.setToNow();
		return now;
	}

	public static class PrefUtils {
		public static String printSelected(Preference pref) {
			if (pref instanceof MultiSelectListPreference) {
				return printSelectedValues((MultiSelectListPreference) pref);
			} else if (pref instanceof ListPreference) {
				return printSelected((ListPreference) pref);
			}

			return "";
		}

		public static String printSelectedValues(MultiSelectListPreference pref) {
			return ToCommaString(getSelectedEntries(pref));
		}

		public static String printSelected(ListPreference pref) {
			return pref.getEntry().toString();
		}

		public static List<String> getSelectedEntries(
				MultiSelectListPreference pref) {
			List<String> list = new ArrayList<String>();

			for (String val : pref.getValues()) {
				CharSequence entry = getEntryFromValue(pref, val);
				list.add(entry.toString());
			}

			return list;
		}

		public static String ToCommaString(List<String> list) {
			StringBuilder commaStr = new StringBuilder();
			for (String val : list) {
				commaStr.append(val + ",");
			}

			if (commaStr.length() > 0) {
				commaStr.deleteCharAt(commaStr.length() - 1);// remove last ,
			}
			return commaStr.toString();
		}

		public static CharSequence getEntryFromValue(
				MultiSelectListPreference pref, String val) {
			// For each value retrieve index
			int index = pref.findIndexOfValue(val);
			// Retrieve entry from index
			return index >= 0 && pref.getEntries() != null ? pref.getEntries()[index]
					: null;
		}

		public static String getEntryFromValue(Context context, int valResId,
				int entryResId, String value) {
			String[] values = context.getResources().getStringArray(valResId);
			String[] entries = context.getResources()
					.getStringArray(entryResId);

			for (int i = 0; i < values.length; i++) {
				if (values[i].equalsIgnoreCase(value)) {
					return entries[i];
				}
			}

			Utils.Log("getEntryFromValue failed for value " + value);
			return "";
		}
	}

	public static int getDiffWithToday(Time time) {

		int inDays = Utils.getDayFromTime(time);
		return getDiffWithToday(inDays);
	}
	
	public static int getDiffWithToday(int inDays) {
		
		int today = Utils.getDayFromTime(Utils.getNow());
		return today - inDays;
	}

}
